package cn.edu.nju.software.action.category;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.CategoryShow;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class GetCategoryNavigationAction extends ActionSupport implements SessionAware {
	
	private int id;
	private List<CategoryShow> navigation;
	private Map<String, Object> session;

	public void setId(int id) {
		this.id = id;
	}

	public List<CategoryShow> getNavigation() {
		return navigation;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		@SuppressWarnings("unchecked")
		Map<Integer, CategoryShow> categoriesMap = (Map<Integer, CategoryShow>) session.get(Constants.SESSION_ALL_CATEGORIES);
		CategoryShow currentCategory = categoriesMap.get(id);
		navigation = new Stack<CategoryShow>();
		do {
			navigation.add(currentCategory);
		} while ((currentCategory = currentCategory.getFatherCategory()) != null);
		navigation.remove(navigation.size()-1);
		Collections.reverse(navigation);
		return SUCCESS;
	}

}
